package com.erp.web.controller.framework;


import com.erp.framework.annotation.Log;
import com.erp.framework.core.controller.BaseController;
import com.erp.framework.core.domain.AjaxResult;
import com.erp.framework.domain.SysUser;
import com.erp.framework.enums.BusinessType;
import com.erp.framework.service.ISysUserService;
import com.erp.framework.shiro.service.SysLoginService;
import com.erp.framework.util.ShiroUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 个人信息 业务处理
 *
 * @author erp
 */
@Controller
@RequestMapping("/framework/user/profile")
public class SysProfileController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SysProfileController.class);

    private final String prefix = "framework/user/profile";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysLoginService loginService;

    /**
     * 个人信息
     */
    @GetMapping()
    public String profile(ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user", user);
        mmap.put("roleGroup", userService.selectUserRoleGroup(user.getId()));
        return prefix + "/profile";
    }

    @GetMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(String password) {
        SysUser user = ShiroUtils.getSysUser();
        return loginService.matches(user, password);
    }

    @GetMapping("/resetPwd")
    public String resetPwd(ModelMap mmap) {
        mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/resetPwd";
    }

    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(String oldPassword, String newPassword) {
        SysUser user = ShiroUtils.getSysUser();
        if (!loginService.matches(user, oldPassword)) {
            return error("修改密码失败，旧密码错误");
        }
        if (loginService.matches(user, newPassword)) {
            return error("新密码不能与旧密码相同");
        }
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(loginService.encryptPassword(user.getUserName(), newPassword, user.getSalt()));

        if (userService.resetUserPwd(user) > 0) {
            ShiroUtils.setSysUser(userService.selectUserById(user.getId()));
            return success();
        }
        return error("修改密码异常，请联系管理员");
    }

    /**
     * 修改用户
     */
    @GetMapping("/edit")
    public String edit(ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        mmap.put("user", userService.selectUserById(user.getId()));
        return prefix + "/edit";
    }


    /**
     * 修改用户
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ResponseBody
    public AjaxResult update(SysUser user) {
        SysUser currentUser = ShiroUtils.getSysUser();
        currentUser.setUserName(user.getUserName());
        currentUser.setEmail(user.getEmail());
        if (userService.update(currentUser) > 0) {
            ShiroUtils.setSysUser(userService.selectUserById(currentUser.getId()));
            return success();
        }
        return error();
    }

}
